package com.zbbt.sportslottery.ui.activity;

import com.gc.materialdesign.views.ButtonRectangle;
import com.google.gson.JsonObject;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.Constant;
import com.zbbt.sportslottery.app.ServerConfig;
import com.zbbt.sportslottery.bean.HuoDongQuanData;
import com.zbbt.sportslottery.net.TypeGsonHttpHandler;
import com.zbbt.sportslottery.net.TypeResultHttpHandler;
import com.zbbt.sportslottery.net.ZbHttpClient;
import com.zbbt.sportslottery.ui.fragment.SaoYiSaoFragment;
import com.zbbt.sportslottery.ui.widget.ClearEditText;
import com.zbbt.sportslottery.util.DialogUtil;
import com.zbbt.sportslottery.util.SharePreferUtil;
import com.zbbt.sportslottery.util.TimeUtil;
import com.zbbt.sportslottery.util.ToastUtil;
import com.zbbt.sportslottery.util.DialogUtil.OnshowUserListener;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;
/**
 * 
 * @ClassName: EXChangeActivity
 * @Description: 兑换码
 * @author: yangwenquan
 * @date: 2015年3月4日 下午4:20:43     
 * @version: V1.0
 */
public class EXChangeActivity extends SimpleBaseActivity {
	private ClearEditText redeemCode;
	private ButtonRectangle exchange;
	private int tiYanQuanId=0;

	@Override
	protected void onActivityCreated(Bundle savedInstanceState) {
		setContentView(R.layout.activity_exchange);
	}

	@Override
	protected void initView() {
		redeemCode = (ClearEditText) findViewById(R.id.redeem_code_edt);
		exchange = (ButtonRectangle) findViewById(R.id.redeem_code_btn);
		exchange.setTextSize(18);
	}

	@Override
	protected void setListener() {
		exchange.setOnClickListener(this);
	}

	@Override
	protected void onClickEvent(View v) {
		switch (v.getId()) {
		case R.id.redeem_code_btn:
			if (TextUtils.isEmpty(redeemCode.getText().toString().trim())) {
				ToastUtil.showLong("请输入兑换码");
				redeemCode.setShakeAnimation();
			} else {
				httpData();
			}
			break;
		default:
			break;
		}
	}
	//TODO 未完成
	private void httpData() {
		JsonObject jsonObject = new JsonObject();
		String couponNum = redeemCode.getText().toString().trim();
		jsonObject.addProperty("couponNum", couponNum);
		ZbHttpClient.postNewApi(ServerConfig.API_HDQXQ, jsonObject, new TypeGsonHttpHandler<HuoDongQuanData>() {

			

			@Override
			public void onSuccess(HuoDongQuanData data) {
				String statusStr = null;
				if (data != null) {
					if (data.getStatus()==1) {
						statusStr = "未使用";
					} else if (data.getStatus()==2) {
						statusStr = "已使用";
					} else if (data.getStatus()==3) {
						statusStr = "已过期";
					}
					tiYanQuanId = data.getId();
					String name = data.getCouponName();
					int price = data.getPrice();
					final String couponNum = data.getCouponNum();
					String getExpire_time = TimeUtil.getTimeDate(data.getEndEffectiveTime());
					DialogUtil.showUserHDQDialog(EXChangeActivity.this, name, price+"",
							couponNum, getExpire_time, statusStr, new OnshowUserListener() {

								@Override
								public void onPositiveButtonClick() {
									
								}

								@Override
								public void onNegativeButtonClick() {
									httpData1(couponNum);
								}

							});
				} else {
					ToastUtil.showLong("兑换码有误!!!");
				}
			}

			@Override
			public void onFailure(String errorCode, String errorMsg) {
				ToastUtil.showLong(errorMsg);
			}
		});
	}

	@Override
	protected void initData() {

	}
	private void httpData1(String couponNum) {
		JsonObject jsonObject = new JsonObject();
		String userId = SharePreferUtil.getString(Constant.USER_ID, "");
		jsonObject.addProperty("couponNum", couponNum);
		jsonObject.addProperty("userId", userId);
		jsonObject.addProperty("couponId", tiYanQuanId);
		ZbHttpClient.postNewApi(ServerConfig.API_TYQDH, jsonObject, new TypeResultHttpHandler() {
			
			@Override
			public void onSuccess() {
				ToastUtil.showLong("兑换成功");
			}
			
			@Override
			public void onFailure(String errorCode, String errorMsg) {
				ToastUtil.showLong(errorMsg);
			}
		});
		
	}
}
